package com.egao.customer.service.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.utils.FileUploadUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
//import com.egao.customer.mapper.CustomerMapper;
import com.egao.customer.biz.PayHistoryBiz;
import com.egao.customer.entity.Customer;
import com.egao.customer.entity.PayHistory;
import com.egao.customer.service.CustomerService;
import org.springframework.stereotype.Service;
import com.egao.customer.biz.CustomerBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 客户管理服务实现类
 * @author : Doman
 * @date : 2020-06-13 13:36:04
 * @version ：1.0
 */
@Service
public class CustomerServiceImpl /*extends ServiceImpl<CustomerMapper, Customer> */implements CustomerService {

	@Autowired
	private CustomerBiz customerBiz;
	@Autowired
	private PayHistoryBiz payHistoryBiz;

    /**
    @Override
    public PageResult<Customer> listPage(PageParam<Customer> page) {
        List<Customer> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<Customer> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JSONObject list(PageResult2<T> page, Customer customer, User loginUser)  throws Exception{
        return customerBiz.list(page,customer,loginUser);
    }

    @Override
    public List<Customer> list(Customer customer, User loginUser) {
        return customerBiz.list(customer,loginUser);
    }

    @Override
    public Customer getById(Customer customer) {
        return customerBiz.getById(customer);
    }

    @Transactional
    @Override
    public JsonResult cardUpdate(Customer customer, PayHistory payHistory, User loginUser) {
        try {
            String card_old = customer.getCard();
            customer.setCard(customer.getChecked());
            if (customerBiz.cardUpdate(customer, loginUser)) {
//                Customer customer1 = customerBiz.getById(customer);
                payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
                payHistory.setType("卡号修改");
                payHistory.setRemark("原卡号:"+card_old+"修改为"+customer.getCard());
                payHistoryBiz.save(payHistory, loginUser);
            }
        } catch (Exception e) {
            return JsonResult.error("卡号重复,不能修改");
        }

        return JsonResult.ok("修改成功");
    }

    @Transactional
    @Override
    public JsonResult jf(Customer customer, PayHistory payHistory, User loginUser) throws ParseException {
        customer.setState("缴费");
        payHistory.setRemark(customer.getRemark());
        int count = 0;
//        customer.setRemark("");
        if (customerBiz.jf(customer, loginUser)) {
            if (Utils.isNotEmpty(customer.getPrice()) && !customer.getPrice().equals("0")&& !customer.getPrice().equals("0.00")&& !customer.getPrice().equals("0.0")) {
                payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
                payHistory.setType("缴费");
                payHistory.setRemark(customer.getRemark()+"_现金缴费"+customer.getAdvanceChargeDiscount()+"折");
                payHistoryBiz.save(payHistory, loginUser);
                count++;
            }
            if (Utils.isNotEmpty(customer.getAdvanceCharge()) && !customer.getAdvanceCharge().equals("0")&& !customer.getAdvanceCharge().equals("0.00")&& !customer.getAdvanceCharge().equals("0.0")) {
                customerBiz.updateYfk(customer, loginUser);
                payHistory.setPrice("-"+customer.getAdvanceCharge());
                payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
                payHistory.setType("预付缴费");
                payHistory.setRemark(customer.getRemark()+"_预付款缴费"+customer.getAdvanceChargeDiscount()+"折");
                payHistoryBiz.save(payHistory, loginUser);
                count++;
            }
            if (count > 0) {
                return JsonResult.ok("缴费成功");
            } else {
                throw new ParseException("缴费失败", 1);
            }
        }
        return JsonResult.error("缴费失败");
    }

//    public static void main(String[] args) throws ParseException {
//        System.out.println(System.currentTimeMillis());
//        SimpleDateFormat sf=new SimpleDateFormat("yyyy");
//        SimpleDateFormat sf2=new SimpleDateFormat("yyyy-MM-dd");
//        String strsystime = sf.format(System.currentTimeMillis());//系统当前时间
//        String strsysnanotime = sf2.format(System.currentTimeMillis());//系统当前时间
//        System.out.println(strsystime+ "    "+ strsysnanotime);
//        Date b = sf2.parse(strsysnanotime);
//        Date a = sf2.parse(strsystime+"-04-01");
//        System.out.println(b.getTime()+"  "+a.getTime());
//
//        String c = "2020-09-07";
//        String d = "2020-04-01 ";
//        Date e = sf2.parse(c);
//        Date f = sf2.parse(d);
//        System.out.println(e.getTime() + " "+f.getTime());
//        System.out.println(e.getTime()<f.getTime());
//
//    }

    @Transactional
    @Override
    public JsonResult tk(Customer customer, PayHistory payHistory, User loginUser) {
        payHistory.setRemark(customer.getRemark());
        payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
        payHistory.setType("退款");
        payHistory.setPrice("-"+payHistory.getPrice());
        payHistoryBiz.save(payHistory, loginUser);
        return JsonResult.ok("退款成功");
    }

    @Transactional
    @Override
    public JsonResult kkf(Customer customer, PayHistory payHistory, User loginUser) {
        payHistory.setRemark(customer.getRemark());
        payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
        payHistory.setType("开口费");
        payHistoryBiz.save(payHistory, loginUser);
        return JsonResult.ok("开口费成功");
    }

    @Transactional
    @Override
    public JsonResult bt(Customer customer, User loginUser) {
        String msg = "停报状态为正常";
        Customer c2 = customerBiz.getById(customer);
        customer.setName(c2.getName());
        if (customerBiz.updateTbState(customer, loginUser)) {
            PayHistory ph = new PayHistory();
            ph.setCustomerId(customer.getId());
            ph.setCompanyId(UserConstant.COMPANY_ID+"");
            ph.setCustomerName(customer.getName());
            if(customer.getTbState().equals("1")) {
                ph.setType("正常");
            } else {
                ph.setType("报停");
                msg = "正常状态变为停报";
            }
            payHistoryBiz.save(ph, loginUser);
            return JsonResult.ok(msg);
        }
        return JsonResult.error("停报失败");
    }

    @Transactional
    @Override
    public JsonResult yfk(Customer customer, PayHistory payHistory, User loginUser) {
        customer.setState("预付款");
//        customer.setRemark("");
        if (customerBiz.yfk(customer, loginUser)) {
            payHistory.setRemark(customer.getAdvanceChargeRemark());
            payHistory.setCompanyId(UserConstant.COMPANY_ID+"");
            payHistory.setType("预付款");
            payHistory.setPrice(customer.getAdvanceCharge());
            payHistory.setPayTime(customer.getAdvanceChargeTime());
            payHistoryBiz.saveYfk(payHistory, loginUser);
            return JsonResult.ok("预付成功");
        }
        return JsonResult.error("预付失败");
    }

    @Transactional
    @Override
    public JsonResult violations(Customer customer, PayHistory ph, User loginUser) {
        customer.setState("违规");
        if (customerBiz.violations(customer, loginUser)) {
            ph.setCustomerId(customer.getId());
            ph.setCompanyId(UserConstant.COMPANY_ID+"");
            ph.setCustomerName(customer.getName());
            ph.setType("违规");
            payHistoryBiz.saveWg(ph, loginUser);
            return JsonResult.ok("设置违规成功");
        }
        return JsonResult.error("设置违规失败");
    }

    @Transactional
    @Override
    public JsonResult save(Customer customer, User loginUser) {
        try {
            try {
                customerBiz.getByCard(customer);
                return JsonResult.error("卡片已添加,请不要重复添加");
            } catch (Exception e) {
            }
            customer.setTbState("1");
            customerBiz.save(customer, loginUser);
            return JsonResult.ok("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error("添加失败");
        }
    }

    @Transactional
    @Override
    public JsonResult update(Customer customer, User loginUser) {
        if (customerBiz.update(customer, loginUser)) {
            String[] pic = customer.getPicRemove().split(",");
            for(String p: pic) {
                File file = new File(FileUploadUtil.UPLOAD_FILE_DIR,  p);
                if (file.delete()) new File(FileUploadUtil.UPLOAD_SM_DIR, p).delete();
            }
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult updateState(Customer customer, User loginUser) {
        if (customerBiz.updateState(customer, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(Customer customer, User loginUser) {
        if (customerBiz.remove(customer, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

    @Transactional
    @Override
    public JsonResult removeByIds(Customer customer, User loginUser) {
        if (customerBiz.remove(customer, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
